import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    meta: {
      isPublic: true
    },
    component: Home
  },
  // 订单
  {
    path: '/order',
    name: 'Order',
    component: () => import('../views/Cart/Order.vue')
  },
  // 购物车
  {
    path: '/cart',
    name: 'Cart',
    props: true,
    component: () => import('../views/Cart/Cart.vue')
  },
  // 详情
  {
    path: '/detail/:id',
    name: 'Details',
    props: true,
    component: () => import('../views/Details.vue')
  },
  // 登录/注册
  {
    path: '/login',
    name: 'Login',
    meta: {
      isPublic: true
    },
    component: () => import('../views/Login/Login.vue')
  },
  {
    path: '/register/:id',
    name: 'register',
    meta: {
      isPublic: true
    },
    component: () => import('../views/Login/register.vue')
  },
  // 地址
  // {
  //   path: '/address/list',
  //   name: 'AddressList',
  //   component: () => import('../views/Address/List.vue')
  // },
  {
    path: '/address/edit',
    name: 'AddressEdit',
    component: () => import('../views/Address/Edit.vue')
  },

]


const router = new VueRouter({
  routes
});

router.beforeEach((to, from, next) => {
  if (!to.meta.isPublic && !localStorage.token) {
    return next("/login");
  }
  next();
});

export default router
